package com.demo.schedule.regular;

import com.alibaba.fastjson.JSON;
import com.demo.entity.DvtDiagnose;
import com.demo.entity.Patient;
import com.demo.enums.MessageRemindEnum;
import com.demo.enums.MessageSourceEnum;
import com.demo.enums.PatientStatusEnum;
import com.demo.mapper.DvtDiagnoseMapper;
import com.demo.mapper.PatientMapper;
import com.demo.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * vte定期提醒线程
 */
@Slf4j
public class RegularDvtMindThread extends Thread {

    private IMessageService messageService;
    private PatientMapper patientMapper;
    private DvtDiagnoseMapper dvtDiagnoseMapper;

    private Patient patient;
    private DvtDiagnose dvtDiagnose;

    public RegularDvtMindThread(Patient patient, DvtDiagnose dvtDiagnose, ApplicationContext applicationContext) {
        this.patient = patient;
        this.dvtDiagnose = dvtDiagnose;
        this.patientMapper = applicationContext.getBean(PatientMapper.class);
        this.dvtDiagnoseMapper = applicationContext.getBean(DvtDiagnoseMapper.class);
        this.messageService = applicationContext.getBean(IMessageService.class);
    }

    @Override
    public void run() {
        log.info("dvt定期提醒");
        log.info("dvt定期提醒 patient = {}", JSON.toJSONString(patient));
        log.info("dvt定期提醒 preventive = {}", JSON.toJSONString(dvtDiagnose));
        try {
            if (!check(patient, dvtDiagnose)) {
                return;
            }
            Map<String, Object> msgContent = new HashMap<>();
            msgContent.put("bed_id", patient.getBedId());
            msgContent.put("bed_name", patient.getBedName());
            msgContent.put("patient_name", patient.getPatientName());
            msgContent.put("dept_name", patient.getDeptName());
            msgContent.put("dept_sn", patient.getDeptSn());
            msgContent.put("message_text", "上次处置建议为“一周后复查超声检查”，建议重新进行 DVT 筛查");
            messageService.sendMessageNotification(MessageRemindEnum.ZHEN_DUAN_REMIND, patient, MessageSourceEnum.OTHER, msgContent, 0);

            Thread.sleep(2 * 60 * 1000);//睡眠 2 分钟
            RegularMindSchedule.addDvtTask(dvtDiagnose);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 发送消息提醒前置检查
     * @return
     */
    public boolean check(Patient patient, DvtDiagnose dvtDiagnose) {
        // 1.病人状态
        patient = patientMapper.queryValidPatient(patient.getPatientId());
        if (patient == null || patient.getStatus() == PatientStatusEnum.CHU_YUAN.getValue()) {
            log.info("当前病人已出院，不在继续进行提醒操作!");
            return false;
        }

        // 2.最新定期提醒评估和当前 preventive 是否是同一个
        DvtDiagnose recentDvtDiagnose = dvtDiagnoseMapper.queryRecentDvtDiagnoseByPatientId(patient.getPatientId());
        if (recentDvtDiagnose == null) {
            log.info("dvt评估记录不存在");
            return false;
        }
        if (recentDvtDiagnose.getDvtDiagnoseId().intValue() > dvtDiagnose.getDvtDiagnoseId().intValue()) {
            log.info("已经有比当前dvt更新的定期提醒评估，不在继续进行提醒操作!");
            return false;
        }
        dvtDiagnose = recentDvtDiagnose;
        return true;
    }
}
